package com.example.medicine.controller;


import com.example.medicine.Bean.mingyi;
import com.example.medicine.Mapper.myxxMapper;
import org.apache.poi.ss.usermodel.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;


@RestController
@RequestMapping("/myxx")
public class MyxxController {
    @Autowired
    private myxxMapper myxxMapper;
    @GetMapping("/")
    public List<mingyi> getMyxxList(){
        return myxxMapper.getall();
    }

    @PostMapping("/")
    public void insertMyxx(@RequestBody mingyi mingyi){
        myxxMapper.insert(mingyi);
    }
    @PostMapping("/batch/")
    public ResponseEntity<String> insertBatchMyxx(@RequestParam("file") MultipartFile file) {
        try {
            // 校验文件类型，只接受 .xls 和 .xlsx 格式的文件
            if (!file.getOriginalFilename().endsWith(".xls") && !file.getOriginalFilename().endsWith(".xlsx")) {
                return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("Unsupported file format. Please upload an Excel file.");
            }

            List<mingyi> mingyiList = new ArrayList<>();
            // 使用 Apache POI 解析 Excel 文件并提取数据
            Workbook workbook = WorkbookFactory.create(file.getInputStream());
            Sheet sheet = workbook.getSheetAt(0);

            for (Row row : sheet) {
                if (row.getRowNum() == 0) { // 跳过表头行
                    continue;
                }
                mingyi mingyi = new mingyi();
                for(int i=0;i<=8;++i){
                    String value=null;
                    Cell cell= row.getCell(i);
                    if(cell!=null){
                        CellType cellType=cell.getCellType();
                        value = switch (cellType) {
                            case STRING ->
                                // 处理字符串类型的单元格
                                    cell.getStringCellValue();
                            case NUMERIC ->
                                // 处理数值类型的单元格
                                    String.valueOf((int)cell.getNumericCellValue());
                            default -> null;
                        };
                    }
                    switch (i) {
                        case 0 -> mingyi.setXingming(value);
                        case 1 -> mingyi.setChaodai(value);
                        case 2 -> mingyi.setJiguan(value);
                        case 3 -> mingyi.setXinyididian(value);
                        case 4 -> mingyi.setYijialiupai(value);
                        case 5 -> mingyi.setTedian(value);
                        case 6 -> mingyi.setShicheng(value);
                        case 7 -> mingyi.setDizi(value);
                        case 8 -> mingyi.setSuoshuoqihou(value);
                    }
                }
                mingyi.setShenhe("待审核");
                mingyi.setCreateuser("admin");
                System.out.println(mingyi);
                mingyiList.add(mingyi);
            }
            // 将数据批量插入数据库
            if (!mingyiList.isEmpty()) {
                myxxMapper.batchInsertDataList(mingyiList);
                System.out.println("成功！");
            }
            return ResponseEntity.ok().body("{\"message\": \"文件提交成功!\"}");
        } catch (Exception e) {
            e.printStackTrace(); // 添加错误日志
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("{\"message\": \"发生错误！请联系管理员！\"}");
        }
    }
    @PutMapping("/")
    public void updateMyxx(@RequestBody mingyi mingyi){
        myxxMapper.update(mingyi);
    }

    @PutMapping("/{id}")
    public void shenheMyxx(@PathVariable int id,@RequestBody mingyi mingyi){
        mingyi.setId(id);
        myxxMapper.updateShenhe(mingyi);
    }

    @DeleteMapping("/{id}")
    public void deleteMyxx(@PathVariable int id){
        myxxMapper.deleteById(id);
    }
}